package com.xhui.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhui.entity.Book;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
public class BookDaoTestCase {

    //使用 @Resource较@Autowired 更加符合JPA(java持久化API Java Persistence API)规范
    @Resource
    private BookDao bookDao;

    @Test
    void testBook(){
        System.out.println(bookDao.getBookById(1));
    }

    @Test
    void testSelectAll(){
        System.out.println(bookDao.getBookById(1));
    }

    @Test
    void testSave(){
        Book book = new Book();
        book.setName("testData2");
        book.setType("testData2");
        book.setDescription("testData2");
        bookDao.insert(book);
    }

    @Test
    void testGetAll(){
        System.out.println(bookDao.selectList(null));
    }

    /**
     *  IPage对象中封装了分页操作中的所有数据
     *  * 数据
     *  * 当前页码值
     *  * 每页数据总量
     *  * 最大页码值
     *  * 数据总量
     */
    @Test
    void testGetPage(){
        IPage page = new Page(2, 5); // 当前页数 条数
        bookDao.selectPage(page, null);
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal()); // 没有限制分页前的总数据条数
        System.out.println(page.getPages()); // 总共多少页数
        System.out.println(page.getRecords()); // 符合条件的数据list集合
    }

    /**
     * 根据条件进行查询数据
     */
    @Test
    void testByCondition(){

        QueryWrapper<Book> qw = new QueryWrapper<>();

        qw.like("name", "Spring");
        bookDao.selectList(qw);
    }

    /**
     * lambda
     */
    @Test
    void testByCondition2(){
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        lqw.like(Book::getName, "Spring");
        bookDao.selectList(lqw);
    }

    @Test
    void testByCondition3(){
        String name = null;
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        lqw.like(Book::getName, name);
        bookDao.selectList(lqw);
    }

    @Test
    void testByCondition4(){
        String name = "1";
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(name) ,Book::getName, name);
        bookDao.selectList(lqw);
    }
}
